<?php
/**
 * Contact Document List
 *
 */
use Nawa\Customer;
use Fox\Document\Type;
use Fox\Msg;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Html\Block;
use Fox\Grid;

$xview = Xview::instance();
$gridName = 'documents';
$customerid = $xview->val('customerid');
$grid = new Grid($gridName,Generator::url('contacts','documents',null,array('customerid' => $customerid)));
$grid->fieldcat(array('docid','filename','doctype','uname','udate','export'));
$grid->setCoche();
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field('docid')->hidden(true);
$grid->fieldcat()->field('doctype')->width(30);
$grid->fieldcat()->setAllFieldsAttr('setHiddenInput', true);
$grid->fieldcat()->setGenerators(array(
	'coche' => Generator::useValue(''),
	'filename' => Generator::useFunction(function($data){
		return $data->document()->filename;
	}),
	'doctype' => Generator::useFunction(function($data){
	return \Fox\Dao::load(Type::className(),$data->document()->doctype)->description;
	}),
	'uname' =>  Generator::useFunction(function($data){
	return $data->document()->uname;
	}),
	'udate' =>  Generator::useFunction(function($data){
		return $data->document()->udate;
	}),
	'export' => Fox\Generator::url(
		"core",
		"files",
		"download",
		array("id" => Fox\Generator::useAttribute('docid'),
			 "xvtrs" => Fox\Generator::useValue("none"))
	)
	));

	$grid->footer()->add(new Button('adnew',Generator::display('addDocument', Generator::url('contacts','document','upload',array(
									"customerid" => $customerid)),"MC_$gridName")));
	$grid->footer()->add(new Button(Grid::BUTTON_DELETE_LINE,Generator::gridDeleteLines($gridName),
	Msg(Fox\Msg::BUNDLE,Fox\Msg::BUT_DELETE)));

	if($xview->val('xvtrs'))
	{
		try {
			$customer = Fox\Dao::load(Customer::className(),$customerid);
			$grid->initialize($customer->documents());
		}
		catch (Fox\Object\Exception\NotExist $e)
		{

		}
		echo $grid->display();
	}
	else
	{
		$block = new Block('bDocuments',Msg(Msg::BUNDLE,Msg::TITLE_DOCUMENTS));
		$block->add($grid);
		return $block;
	}